﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZhiAo.CommonLibrary
{
    public static class DataExtend
    {
        public static string ToMyString(this double? key)
        {
            if (key.HasValue)
            {
                return key.Value.ToString();
            }
            else
            {
                return "";
            }
        }
 

        /// <summary>
        /// 浮点型转化为布尔型 1 True 其他 false
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool ToBool(this double? key)
        {
            double value = 0;
            if (key.HasValue)
            {
                value = key.Value;
            }
            else
            {
                value = 0;
            }

            return value == 1;
        }


        public static int ByteLength(this string key)
        {
            return System.Text.Encoding.GetEncoding("gb2312").GetByteCount(key);
        }

        public static string ToMyString(this string key)
        {
            if (string.IsNullOrEmpty(key)) return "";
            else return key;
        }

        public static string ToInSQL(this string key)
        {
            string result = "";
            string[] array = key.Split(',');
            foreach (string item in array)
            {
                result += ",'" + item+"'";
            }

            result = "("+result.Substring(1)+")";

            return result;
        }


        public static int ToInt(this string key)
        {
            return key.ToInt("数据格式不正确,要求整数");//数据格式不正确,要求整数
        }

        public static int ToInt(this string key, string errorMessage)
        {
            int result = 0;
            if (!int.TryParse(key, out result)) throw new Exception(errorMessage);
            return result;
        }

        public static DateTime ToDate(this string key, string message)
        {
            DateTime date = DateTime.Now;
            if (key == "") throw new Exception("请输入" + message);//请输入 
            if (!DateTime.TryParse(key, out date)) throw new Exception(message + "格式不正确");//格式不正确
            return date;
        }

        public static DateTime ToDate(this string key)
        {
            string message = "日期";// 日期;
            return ToDate(key, message);
        }

        public static DateTime? ToDateIgnoreNull(this string key, string message)
        {
            DateTime date = DateTime.Now;
            if (key == "")
            {
                return null;
            }
            if (!DateTime.TryParse(key, out date)) throw new Exception(message + "格式不正确");//格式不正确
            return date;
        }

        public static DateTime ToDateIgnoreNull(this string key)
        {
            string message = "日期";// 日期;
            return ToDate(key, message);
        }

        public static int ToIntIgnoreError(this double? key)
        {
            if (key.HasValue)
            {
                return Convert.ToInt32(key.Value);
            }
            else
            {
                return 0;
            }
        }

        public static int ToIntIgnoreError(this string key)
        {
            int value = 0;
            int.TryParse(key, out value);
            return value;
        }

        public static double ToDoubleIgnoreError(this string key)
        {
            double value = 0;
            double.TryParse(key, out value);
            return value;
        }


        public static double ToDoubleIgnoreError(this double? key)
        {
            if (key.HasValue)
            {
                return key.Value;
            }
            else
            {
                return 0;
            }
        }



        public static int ToInt(this bool key)
        {
            
            return key ? 1 : 0;
        }

        public static string ToYN(this bool key)
        {
            return key ? "Y" : "N";
        }

        public static void Alert(this  string key)
        {
            HtmlWindow.AlertSuccess(key);
        }

        public static void Alert(this Exception ex)
        {
            HtmlWindow.Alert(ex);
        }

   }
}
